package shapewar.engine.presenter;

import java.util.Timer;
import java.util.TimerTask;

import shapewar.conf.Conf;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

public abstract class EngineThread extends TimerTask {
	private boolean bo = false;
	private Handler handler = null;
	private Timer time = null;
	public EngineThread(){
		this(Conf.FPS);
	}
	@SuppressLint("HandlerLeak") 
	public EngineThread(int fps){
		handler = new Handler(){
			public void handleMessage(Message what){
				repeat();
			}
		};
		time = new Timer();
		time.schedule(this,0,1000/fps);
	}
	public abstract void repeat();
	public void start(){
		bo = true;
	}
	public void stop(){
		bo = false;
	}
	@Override
	public void run()
	{
		if(bo) {
			handler.sendEmptyMessage(0);
		}
	}
	public void destory() {
		time.cancel();
		this.cancel();
		try {
			this.finalize();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}